/**
 * StringUtil.java - 2013-4-17
 *
 * Licensed Property to China UnionPay Co., Ltd.
 * 
 * (C) Copyright of China UnionPay Co., Ltd. 2010
 *     All Rights Reserved.
 *
 * Project Info: China UnionPay Internet Acquiring Project
 * 
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *        
 * =============================================================================
 */
package com.lijin.batchsys.framework.common;

import java.text.MessageFormat;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.ArrayUtils;



/**
 * Description: 
 *
 * (C) Copyright of China UnionPay Co., Ltd. 2010.
 *
 */
public class StringUtil extends org.apache.commons.lang.StringUtils {
	
	public static final String DEFAULT_SPLITER = ",";
	
	private static final String zh_regEx = "[\u4e00-\u9fa5]"; // 中文字符正则表达式

	/**
	 * 将字符串数组trim后拼接成字符串，用默认的分隔符进行分隔
	 * @param strArray
	 * @return
	 */
	public static String strArrToString(String[] strArr) {
		AssertUtil.argIsNull(strArr, "参数strArr为null");
		return strArrToString(strArr, DEFAULT_SPLITER);
	}
	
	/**
	 * 将数组trim后拼接成字符串,用指定的分隔符进行分隔
	 * @param strArray
	 * @return
	 */
	public static String strArrToString(String[] strArr, String spliter) {
		AssertUtil.argIsNull(strArr, "参数strArr为null");
	
		// 如果未指定分隔符，或者指定的分隔符字符串为空，则用默认的分隔符进行分隔
		if (isBlank(spliter)) {
			spliter = DEFAULT_SPLITER;
		}
		
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < strArr.length; i ++) {
			sb.append(strArr[i].trim());
			if (i < strArr.length - 1) {
				sb.append(spliter);
			}
		}
		return sb.toString();
	}

	/**
	 * 将以下划线分隔的字符串按驼峰命令法进行转换
	 * @param strOrg
	 * @return
	 */
	public static String camelCase(String strOrg) {
		String[] list = StringUtil.lowerCase(strOrg).split("_");
		StringBuilder buf = new StringBuilder(strOrg.length());
		for(String word : list) {
			buf.append(word);
			int firstLetterIndex = buf.length()-word.length();
			if( firstLetterIndex != 0) {
				buf.setCharAt(firstLetterIndex, Character.toUpperCase(buf.charAt(firstLetterIndex)));
			}
		}
		return buf.toString();
	}

	/**
	 * 格式化输出msg
	 * @param msg
	 * @param args
	 * @return
	 */
	public static String formatMessage(String msg, Object... args) {
		if (isBlank(msg) || ArrayUtils.getLength(args) == 0) {
			return msg;
		} else {
			return MessageFormat.format(msg, args);
		}
	}
	
	/**
	 * 根据指定长度初始化CHAR_MAP
	 * @param length
	 * @return
	 */
	public static String initCharMap(int length) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i ++) {
			sb.append("0");
		}
		return sb.toString();
	}
	
	/**
	 * 将Set转成逗号分隔的字符串
	 * @param strSet
	 * @return
	 */
	public static String setToString(Set<String> strSet) {
		if (strSet != null && !strSet.isEmpty()) {
			return strArrToString(strSet.toArray(new String[] { }));
		}
		return "";
	}
	
	/**
	 * 将Set转成按指定字符分隔的字符串
	 * @param strSet
	 * @return
	 */
	public static String setToString(Set<String> strSet, String spliter) {
		if (strSet != null && !strSet.isEmpty()) {
			return strArrToString(strSet.toArray(new String[] { }), spliter);
		}
		return "";
	}
	
	/**
	 * 检查是否包含中文信息
	 * @param str
	 * @return
	 */
	public static boolean containsZhCn(String str) {
		if (isBlank(str)) {
			return false;
		}
		Matcher matcher = Pattern.compile(zh_regEx).matcher(str);
		return matcher.find();
	}
	
	/**
	 * 计算字符串长度，如果是中文，则要乘以3
	 * @param str
	 * @return
	 */
	public static int strLen(String str, String charSet) {
		int len = 0;
		if (containsZhCn(str)) {
			try {
				if (StringUtil.isBlank(charSet)) {
					charSet = "UTF-8";
				}
				len = str.getBytes(charSet).length;
			} catch (Exception e) {
				throw new IllegalStateException("获取字符串长度失败" + str);
			}
		} else {
			len = str.length();
		}
		return len;
	}
	
	public static void main(String[] args) {
		Set<String> st = new HashSet<String>();
		st.add("EBPP");
		st.add("UPOP");
		System.out.println(setToString(st));
		
		System.out.println(containsZhCn("eeee"));
		System.out.println(containsZhCn("李ee里"));
		System.out.println(strLen("李ee里", null));
		System.out.println(strLen("李ee里", "UTF-8"));
		System.out.println(strLen("李ee里", "GBK"));
		System.out.println(strLen("李ee里", "GB2312"));
	}
}
